package com.mrd.evaluate.service;

import com.mrd.base.BaseBiz;
import com.mrd.evaluate.entity.Evaluation;
import com.mrd.evaluate.repository.EvaluationDao;
import com.mrd.exception.QueryException;
import com.mrd.framework.ui.Jqgrid;
import com.mrd.framework.ui.filter.SearchRule;
import com.mrd.framework.ui.filter.SearchRuleOption;
import com.mrd.spring.repository.BaseRepository;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

/**
 * Created by lenovo on 2016/8/30.
 */
@Service
public class EvaluationBiz extends BaseBiz<Evaluation> {
    @Resource
    private EvaluationDao evaluationDao;
    @Resource
    private EvaluationRecordBiz evaluationRecordBiz;

    public Evaluation findByRelationId(String relationId) {
        if (StringUtils.isNotEmpty(relationId)) {
            Jqgrid jqgrid = new Jqgrid();
            jqgrid.addRule(new SearchRule("relationId", SearchRuleOption.eq, relationId));
            try {
                Page<Evaluation> evaluationPage = super.findByJqgrid(jqgrid);
                if (evaluationPage != null) {
                    List<Evaluation> evaluationList = evaluationPage.getContent();
                    if (evaluationList != null && evaluationList.size() > 0) {
                        return evaluationList.get(0);
                    }
                }
            } catch (QueryException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public Map<String, Evaluation> findEvaluationMapWithBusinessIds(List<String> businessIds) {
        List<Evaluation> evaluations = evaluationDao.findByRelationIdIn(businessIds);
        Map<String, Evaluation> evaluationMap = new HashMap<>();
        for (Evaluation evaluation : evaluations) {
            evaluationMap.put(evaluation.getRelationId(), evaluation);
        }

        return evaluationMap;
    }

    @Resource
    // 该处指定注入BaseRepository的dao对象
    @Qualifier("evaluationDao")
    public void setBaseRepository(BaseRepository<Evaluation, String> baseRepository) {
        super.baseRepository = baseRepository;
    }
}
